package view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Icon;

import imageUtility.IImageDescriptor;
import imageUtility.ImageData;
import imageUtility.ImageIntensityDescriptor;
import imageUtility.ImageMomentsDescriptor;
import imageUtility.ImagePlacer;

public class Shell {

	JFrame frame;
	private ClickListener clicker = new ClickListener();
	private JButton btnDown;
	private JButton btnUp;
	private AbstractButton btnLeft;
	private JButton btnRight;
	private JLabel top;
	private JLabel topRight;
	private JLabel left;
	private JLabel middle;
	private JLabel right;
	private JLabel bottomLeft;
	private JLabel bottom;
	private JLabel bottomRight;
	private JLabel topLeft;
	private IInfraGuiAdapter adapter = null;
	private Object matrix;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		//IImageDescriptor descriptor = new ImageIntensityDescriptor(10);
		IImageDescriptor descriptor = new ImageMomentsDescriptor(10);
		List<ImageData> imageDatas = new ArrayList<ImageData>();
		
		System.out.println("progress - starting...");
		
		//File[] files = new File("C:\\Users\\Oleg\\Desktop\\Study\\10000.HighDefinition.PNG.Icon.Pack\\Misc. Icons").listFiles();
		File[] files = new File("C:\\Users\\Oleg\\Desktop\\Study\\10000.HighDefinition.PNG.Icon.Pack\\Test").listFiles();
		for(int i = 0; i < files.length; i++) {
			File file = files[i];
			//System.out.println(file.getAbsolutePath());
			double[] desc = descriptor.getImageDescriptors(file.getAbsolutePath());
			try {
				imageDatas.add(new ImageData(file.getAbsolutePath(),desc));
			} catch (Exception e) {
				System.out.println(file.getAbsolutePath());
			}
		}
		
		System.out.println("progress - finished reading images and set descriptors");

		
		ImagePlacer placer = new ImagePlacer(imageDatas, 5);
		ImageData[][] matrix = placer.getPlacedImagesMatrix();
		for (int i=0; i<matrix.length;i++){
			for (int j=0; j<matrix[i].length; j++){
				if (matrix[i][j] != null){
					System.out.println("i="+i+",j="+j+",path="+matrix[i][j].getImagePath());
				}
			}
		}
		EventQueue.invokeLater(new Worker(matrix));
	}

	/**
	 * Create the application.
	 * @param matrix2 
	 */
	public Shell(ImageData[][] matrix) {
		adapter = new GuiAdapter(matrix);
		adapter.Init(matrix.length/2, matrix.length/2);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 500, 439);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		btnDown = new JButton("Down");
		btnDown.addActionListener(clicker);
		btnDown.setBounds(196, 363, 89, 23);
		frame.getContentPane().add(btnDown);

		btnUp = new JButton("Up");
		btnUp.addActionListener(clicker);
		btnUp.setBounds(196, 11, 89, 23);
		frame.getContentPane().add(btnUp);

		btnLeft = new JButton("Left");
		btnLeft.addActionListener(clicker);
		btnLeft.setBounds(10, 128, 51, 23);
		frame.getContentPane().add(btnLeft);

		btnRight = new JButton("Right");
		btnRight.addActionListener(clicker);
		btnRight.setBounds(409, 128, 57, 23);
		frame.getContentPane().add(btnRight);

		ImageIcon icon = new ImageIcon("C:\\Users\\Oleg\\Desktop\\Study\\10000.HighDefinition.PNG.Icon.Pack\\Misc. Icons\\0.png");
		topLeft = new JLabel(icon);
		topLeft.setBounds(71, 45, 89, 96);
		frame.getContentPane().add(topLeft);
		
		top = new JLabel(icon);
		top.setBounds(196, 45, 89, 96);
		frame.getContentPane().add(top);
		
		topRight = new JLabel(icon);
		topRight.setBounds(310, 45, 89, 96);
		frame.getContentPane().add(topRight);
		
		left = new JLabel(icon);
		left.setBounds(71, 152, 89, 96);
		frame.getContentPane().add(left);
		
		middle = new JLabel(icon);
		middle.setBounds(196, 152, 89, 96);
		frame.getContentPane().add(middle);
		
		right = new JLabel(icon);
		right.setBounds(310, 152, 89, 96);
		frame.getContentPane().add(right);
		
		bottomLeft = new JLabel(icon);
		bottomLeft.setBounds(71, 253, 89, 96);
		frame.getContentPane().add(bottomLeft);
		
		bottom = new JLabel(icon);
		bottom.setBounds(196, 253, 89, 96);
		frame.getContentPane().add(bottom);
		
		bottomRight = new JLabel(icon);
		bottomRight.setBounds(310, 253, 89, 96);
		frame.getContentPane().add(bottomRight);


	}

	class ClickListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String[][] newImages = null;
			if (arg0.getSource() == btnLeft) {
				newImages = adapter.moveLeft();
			}
			if (arg0.getSource() == btnRight) {
				newImages = adapter.moveRight();
			}
			if (arg0.getSource() == btnUp) {
				newImages = adapter.moveUp();
			}
			if (arg0.getSource() == btnDown) {
				newImages = adapter.moveDown();
			}
			SyncImages(newImages);
		}

		private void SyncImages(String[][] newImages) {
			topLeft.setIcon(	scaleImage(new ImageIcon(newImages[0][2])));
			top.setIcon(		scaleImage(new ImageIcon(newImages[1][2])));
			topRight.setIcon(	scaleImage(new ImageIcon(newImages[2][2])));
			left.setIcon(		scaleImage(new ImageIcon(newImages[0][1])));
			middle.setIcon(		scaleImage(new ImageIcon(newImages[1][1])));
			right.setIcon(		scaleImage(new ImageIcon(newImages[2][1])));
			bottomLeft.setIcon(	scaleImage(new ImageIcon(newImages[0][0])));
			bottom.setIcon(		scaleImage(new ImageIcon(newImages[1][0])));
			bottomRight.setIcon(scaleImage(new ImageIcon(newImages[2][0])));
		}
		
		private ImageIcon scaleImage(ImageIcon im) {
			return new ImageIcon(im.getImage().getScaledInstance(89, 96, java.awt.Image.SCALE_SMOOTH));
			
		}

	}
	

}
class Worker implements Runnable {

	ImageData[][] mat;
	
	public Worker(ImageData[][] matrix) {
		mat = matrix;
	}
	@Override
	public void run() {
		try {
			Shell window = new Shell(mat);
			window.frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
